package com.tsingteng.cosfun.widget.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.app.App;
import com.tsingteng.cosfun.app.DialogListener;
import com.tsingteng.cosfun.bean.AttentFansbean;
import com.tsingteng.cosfun.ui.cosfun.AtFragment;
import com.tsingteng.cosfun.utils.KeywordUtil;
import com.tsingteng.cosfun.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class CommentPlayDialog extends DialogFragment implements TextWatcher, View.OnClickListener{

    //点击发表，内容不为空时的回调
    public CommentPlayDialogListener commentDialogListener;

    EditText et;
    TextView at;
    TextView send;
    String playId = "";
    List<AttentFansbean.Data> atList = null;
    AttentFansbean.Data data = null;
    private Dialog dialog;
    String contentMsg;
    private String mContent;
    public CommentPlayDialog() {
    }


    @SuppressLint("ValidFragment")
    public CommentPlayDialog(String playId, AttentFansbean.Data data, CommentPlayDialogListener commentDialogListener) {
        this.playId = playId;
        this.data = data;
        this.commentDialogListener = commentDialogListener;
        atList = new ArrayList<>();
    }


    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        dialog = new Dialog(getActivity(), R.style.Comment_Dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        View contentView = View.inflate(getActivity(), R.layout.bottom_play_input_dialog, null);
        dialog.setContentView(contentView);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消

        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.alpha = 1;
        lp.dimAmount = 0.0f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        et = contentView.findViewById(R.id.bottom_input_dialog_et);
        if (!TextUtils.isEmpty(mContent)) {
            et.setText(mContent);
            et.setSelection(mContent.length());
        }
        at = contentView.findViewById(R.id.bottom_input_dialog_at);
        send = contentView.findViewById(R.id.bottom_input_dialog_send);
        at.setOnClickListener(this);
        send.setOnClickListener(this);

        et.addTextChangedListener(this);
        if (data != null) {
            atList.add(data);
            String s = et.getText().toString() + "@" + data.nickName;
            et.setText(s);
        }
        return dialog;
    }


    public void cleanText() {
    }

    int l = 0;//记录字符串被删除字符之前，字符串的长度
    int location = 0;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        l = s.length();
        location = et.getSelectionStart();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        LogUtils.e("testcomm", "txt=" + s);

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (l != s.toString().length()) {
            String input = s.toString();
            SpannableString msp = KeywordUtil.matcherAtTxt(Color.parseColor("#42DAD7"), input, atList);
            et.setText(msp);
            et.setSelection(et.getText().toString().length());
        }
        if (s.toString().length() >= 200) {
            Toast.makeText(App.getContext(), "最多可以评论200个字呦", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bottom_input_dialog_send:
                if (TextUtils.isEmpty(et.getText().toString())) {
                    Toast.makeText(getActivity(), "评论内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                contentMsg = et.getText().toString();
                for (AttentFansbean.Data d : atList) {
                    String s = "<nickName@" + d.nickName + "&profileId@" + d.profileId + "%cosfun>";
                    String o = "@" + d.nickName;
                    if (contentMsg.indexOf(s) < 0) {//如果已经替换则跳过
                        contentMsg = contentMsg.replace(o, s);
                    }

                }
                LogUtils.e("testcomm", "contentMsg=" + contentMsg);
                commentDialogListener.commSuccess(contentMsg);
                dismiss();
                break;
            case R.id.bottom_input_dialog_at:
                AtFragment atFragment = AtFragment.newInstance();
                atFragment.setOnItemSelectLisenter(new AtFragment.OnItemSelectLisenter() {
                    @Override
                    public void selected(AttentFansbean.Data data) {
                        atList.add(data);
                        String s = et.getText().toString() + "@" + data.nickName;
                        et.setText(s);
                        atFragment.dismiss();
                    }
                });
                atFragment.show(getActivity().getSupportFragmentManager(), "myFragment");
                break;
        }
    }

//    /**
//     * 确认弹窗
//     */
//    private void confirmDialog() {
//        @SuppressLint({"StringFormatInvalid", "LocalSuppress"})
//        String content = Objects.requireNonNull(getActivity()).getResources().getString(R.string.comment_black, mNickname);
//        DialogUtil.getContentDialog(getActivity(), content);
//    }
//
//    /**
//     * 拉黑别人
//     */
//    private void confirmBlackDialog(String msg) {
//        @SuppressLint({"StringFormatInvalid", "LocalSuppress"})
//        String content = getResources().getString(R.string.comment_handle, mNickname);
//        DialogUtil.getContentCommentDialog(getActivity(), content, mProfileId, dialogListener);
//    }

    public interface CommentPlayDialogListener {
        void commSuccess(String commStr);
    }

}
